package com.shuangyu.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.shuangyu.common.R;
import com.shuangyu.entity.AddressBook;
import com.shuangyu.service.AddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/addressBook")
@Slf4j
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService;

    /**
     * 查询地址
     * @param request
     * @return
     */
    @GetMapping("/list")
    public R<List<AddressBook>> list(HttpServletRequest request){
        
        String phone = request.getSession().getAttribute("phone").toString();

        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AddressBook::getPhone, phone);

        List<AddressBook> list = addressBookService.list(queryWrapper);

        return R.success(list);
    }

    /**
     * 新增地址
     * @param request
     * @param addressBook
     * @return
     */
    @PostMapping
    public R<String> save(HttpServletRequest request, @RequestBody AddressBook addressBook){

        addressBook.setUserId((long) request.getSession().getAttribute("user"));

        addressBookService.save(addressBook);

        return R.success("操作成功");
    }

    /**
     * 更新默认地址
     * @param request
     * @param addressBook
     * @return
     */
    @PutMapping("/default")
    public R<String> updateByDefault(HttpServletRequest request, @RequestBody AddressBook addressBook){

        addressBook.setUserId((long) request.getSession().getAttribute("user"));
        addressBookService.updateByDefault(addressBook);

        return R.success("操作成功");
    }

    /**
     * 根据id查询地址
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<AddressBook> getById(@PathVariable String id){

        AddressBook addressBook = addressBookService.getById(id);

        return R.success(addressBook);
    }

    /**
     * 更新地址
     * @param addressBook
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody AddressBook addressBook){

        log.info("============{}", addressBook.toString());
        addressBookService.updateById(addressBook);

        return R.success("操作成功");
    }

    /**
     * 删除地址
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<String> delete(String ids){

        String[] idss = ids.split(",");

        List<String> list = Arrays.stream(idss).collect(Collectors.toList());
        addressBookService.removeByIds(list);

        return R.success("操作成功");
    }

    /**
     * 获取默认地址
     * @return
     */
    @GetMapping("/default")
    public R<AddressBook> getByDefault(HttpServletRequest request){
        Long userId = (long) request.getSession().getAttribute("user");
        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AddressBook::getIsDefault, 1).eq(AddressBook::getUserId, userId);
        AddressBook addressBook = addressBookService.getOne(queryWrapper);

        return R.success(addressBook);
    }
}
